/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *  
 * @Original Author: jin Chen
 * @date: Sep 10, 2004$
 * @version: 1.0
 */
package edu.psu.geovista.app.dataloader.v1.actions;


import edu.psu.geovista.app.pcp.toolkits.table.ConfigureTable;
import edu.psu.geovista.app.pcp.toolkits.table.actions.BasicTableAction;
import edu.psu.geovista.common.utils.collection.CollectionUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.HashSet;

public class PickCategoryAction extends BasicTableAction {

    String columnName;// 
    /**
     *
     * @param name
     * @param owner   A dialog box is to prompt up, it need the owner(Must be JComponent) to show up
     * @param table
     */
    public PickCategoryAction(String name, JComponent owner, ConfigureTable table) {
        super(name,owner,table);

    }

    public void actionPerformed(ActionEvent e) {
        if(this.getColumnName()==null||this.getColumnName().trim().equals("") )return;
        Object[] ctgCol = table.getColumnValues(this.getColumnName() );
        if(ctgCol==null||ctgCol.length <=0){
            //unable to find the column
            String msg = "The Wizard was unable to find category information in your data."
                    +"\n Make sure your metadata file has a column named \"category_a\"";

            //JOptionPane.showMessageDialog(owner,msg);
            JOptionPane.showMessageDialog(owner,msg,"Caution",JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        //* get categories' name
        HashSet ctgs=new HashSet();
        for (int i=0;i<ctgCol.length ;i++){
            ctgs.add(ctgCol[i]);
        }
        Object[] ctgNames = CollectionUtils.convert2Array(ctgs);



        //* construct dialog to list the names
        JComboBox comb=new JComboBox(ctgNames);
        Object[] btns=new Object[]{"OK","Cancel"};
        int value=JOptionPane.showOptionDialog(owner, comb,"Please select a category",
                                                JOptionPane.YES_NO_OPTION,
                                                JOptionPane.PLAIN_MESSAGE ,
                                                null,
                                                btns,
                                                btns[0]);
        String selectedCategory=null;
        if(value==JOptionPane.OK_OPTION ){
                             //JOptionPane.OK_OPTION map to  "Summary" button
                             selectedCategory = (String) comb.getSelectedItem();
                            // System.out.println("value:"+selectedCategory );


                         }
                         else  if(value==JOptionPane.NO_OPTION ||
                                      value==JOptionPane.CLOSED_OPTION ){
                             //JOptionPane.NO_OPTION map to "Cancel" button
                             //System.out.println("cancel");
                             return;

                         }


        //* find the rows belong to the category
         HashSet index=new HashSet();
         for (int i=0;i<ctgCol.length ;i++){
             String  actg = (String) ctgCol[i];
             if(actg.equals(selectedCategory) ){
                  index.add(new Integer(i));
             }
         }

                int[] ints = CollectionUtils.convert2IntArray(index); // row index of the record which belong to the category


        //* move to top
                table.moveTop(ints);
                table.selectRows(0,ints.length-1);



            }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }


    }

